﻿using Microsoft.Xna.Framework;

namespace PoolGame2.Physics
{
    public class PhysicsState
    {
        private Vector3[] oldballPositions;

        private PoolGame2.Game.Cue cue;

        private Ball[] balls;
        private Cushion[] cushions;
        private Pocket[] pockets;
        private int ballCount = 0;
        private int pocketCount = 0;
        private int cushionCount = 0;

        public PhysicsState(PoolGame2.Game.Cue cue)
        {
            balls = new Ball[16];
            oldballPositions = new Vector3[16];
            cushions = new Cushion[6];
            pockets = new Pocket[6];
            this.cue = cue;
        }

        public Ball GetCueball()
        {
            return balls[0];
        }

        public PoolGame2.Game.Cue getCue()
        {
            return cue;
        }

        public Cushion[] getCushions()
        {
            return this.cushions;
        }

        public Ball[] getBalls()
        {
            return balls;
        }

        public Pocket[] getPockets()
        {
            return pockets;
        }

        public void AddPocket(Pocket p)
        {
            pockets[pocketCount++] = p;
        }

        public void AddCushion(Cushion c)
        {
            cushions[cushionCount++] = c;
        }

        public void AddBall(Ball b)
        {
            balls[ballCount++] = b;
        }

        public void RecordOldBallPositions()
        {
            for (int i = 0; i < balls.Length; i++)
            {
                oldballPositions[i] = balls[i].getPosition();
            }
        }

        public void RestoreBallPositions()
        {
            for (int i = 0; i < balls.Length; i++)
            {
                balls[i].setPosition(oldballPositions[i]);
                // reset the velocities to zero
                balls[i].setVelocity(Vector3.Zero);
                balls[i].angularVelocity = Vector3.Zero;
            }
        }

    }
}
